### 
#Usage: Rscript GTEx_plot.R -jt GTEx_plot.csv -log TRUE
#       Rscript GTEx_plot.R -jt GTEx_plot.csv -log FALSE

#       Rscript GTEx_plot.R -jt GTEx_plot1.csv -log TRUE
#       Rscript GTEx_plot.R -jt GTEx_plot1.csv -log FALSE
###

library(getopt)
library(ggpubr)
library(dplyr)
spec <- matrix(
  c("junction_table",  "jt", 2, "character", "This is cpm table!",
    "log_scale", "log", 2, "character",  "whether log!"),
  byrow=TRUE, ncol=5)
args=getopt(spec)
data = read.table(args$junction_table, header=F, row.names = NULL, sep = ",")
colnames(data)=c("Tissue","CPM")
if (args$log=="TRUE"){data$CPM = log2(data$CPM+1)}
#colnames(data)

############## calculate the sample number of each group ###########
data = data %>% 
  tbl_df() %>% 
  group_by(Tissue) %>% 
  mutate(sampleNum =max(row_number()))
data$Label=paste(data$Tissue,"(",data$sampleNum,")",sep="")
data = as.data.frame(data)
data$Label = as.factor(data$Label)

########### calculate the median of each group
medians = data %>%
  group_by(Label) %>%
  summarise(median = median(CPM))
myorder = medians$Label[order(-medians$median)]

if (args$log=="TRUE"){
#width=(600*5)/(72*4)=10.41667英寸 height=(600*3)/(72*4)=6.25英寸
#png(file="GTEx_plot_log.png",width=600*5,height=600*3,res=72*4)
pdf(file="GTEx_plot_log.pdf", width=10.5, height=6.5)
### ！！！！box-plot的颜色设置
colors <- colorRampPalette(c("purple", 'green', 'yellow',"#FF3399"))(30)
data$Label=factor(data$Label, levels=myorder)  ## 设置箱线的X轴顺序
p = ggboxplot(data, x="Label", y="CPM", color = "black", fill = colors[rank(30:1)], outlier.size = 0.5, legend="none", size = 0.2)+
  #scale_x_discrete(limits=myorder)+
  geom_hline(yintercept = median(data$CPM), linetype=2, color="black", size=0.5)+
  xlab("")+
  theme(axis.title.x = element_text(size = 10, face = "bold"))+
  ylab("log2(CPM+1)")+
  scale_y_continuous(expand = c(0, 0))+
  theme(axis.title.y = element_text(size = 10, face = "bold"))+
  theme(axis.text.x = element_text(size =7,angle = 60, hjust = 1, face="bold"))+
  theme(axis.text.y = element_text(size =7,angle = 360, hjust = 1, face="bold"))+
  theme(axis.line = element_line(size = 0.5))+
  theme(panel.background = element_rect(colour = "black",size =1))
print(p)
dev.off()
}else{
pdf(file="GTEx_plot_noLog.pdf", width=10.5, height=6.5)
### ！！！！box-plot的颜色设置
colors <- colorRampPalette(c("purple", 'green', 'yellow',"#FF3399"))(30)
data$Label=factor(data$Label, levels=myorder)  ## 设置箱线的X轴顺序
p = ggboxplot(data, x="Label", y="CPM", color = "black", fill = colors[rank(30:1)], outlier.size = 0.5, legend="none", size = 0.2)+
  scale_x_discrete(limits=myorder)+
  geom_hline(yintercept = median(data$CPM), linetype=2, color="black", size=0.5)+
  xlab("")+
  theme(axis.title.x = element_text(size = 10, face = "bold"))+
  ylab("CPM")+
  scale_y_continuous(expand = c(0, 0))+
  theme(axis.title.y = element_text(size = 10, face = "bold"))+
  theme(axis.text.x = element_text(size =7,angle = 60, hjust = 1, face="bold"))+
  theme(axis.text.y = element_text(size =7,angle = 360, hjust = 1, face="bold"))+
  theme(axis.line = element_line(size = 0.5))+
  theme(panel.background = element_rect(colour = "black",size =1))
print(p)
dev.off()
}




